﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;
using System.Text;

namespace BIS.Portal.Modules.CMS
{
    public class ImageColection
    {
        private int article_id;
        private string[] image_thumb;
        private string[] tilte_img;
        private string[] description;

        public int ArticleId
        {
            get { return article_id; }
            set { article_id = value; }
        }

        public string[] ImageThumb
        {
            get { return image_thumb; }
            set { image_thumb = value; }
        }

        public string[] TilteImg
        {
            get { return tilte_img; }
            set { tilte_img = value; }
        }

        public string[] Description
        {
            get { return description; }
            set { description = value; }
        }
    }

    public partial class CmsMediaList : ControlBase
    {

        #region Load data properties
        public string SLMediaList
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaList.txtSLMediaList, PortalContext.LanguageAlias); }
        }
        public bool HienThiTieuDeTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaList.ddlMediaTieuDe, PortalContext.LanguageAlias); }
        }
        public bool HienThiMoTaTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaList.ddlMediaMoTa, PortalContext.LanguageAlias); }
        }
        public bool HienThiLinkCtTin
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaList.ddlMediaLinkCt, PortalContext.LanguageAlias); }
        }
        public string SLPopupAnh
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaList.txtSLAnhPopup, PortalContext.LanguageAlias); }
        }
        public string SLPopupVideo
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaList.txtSLVideoPopup, PortalContext.LanguageAlias); }
        }
        public string SLPopupFile
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaList.txtSLFilePopup, PortalContext.LanguageAlias); }
        }
        #endregion

        private string GetCateName(int iCateId)
        {
            return ClientCmsArticleCategoryAndCategoryBO.GetCategoryNameById(iCateId).Category_name;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            loadData();
        }
        private void loadData()
        {

            int CateId = Functions.GetInt(Request[ModuleQueryStringKey.CategoryId]);
            int page_index = Functions.GetInt(Request[ModuleQueryStringKey.PageIndex]);
            int row_num = Functions.GetInt(SLMediaList);
            IList<ClientCmsArticleAndCategoryEntity> list = ClientCmsArticleBO.GetArticleByCategoryId(CateId, PortalContext.LanguageInUseId, page_index - 1, row_num);
            if (list.Count > 0)
            {
                Nhom_MeDia.DataSource = list;
                Nhom_MeDia.DataBind();

                this.PagingControl.QueryStringName = ModuleQueryStringKey.PageIndex;
                this.PagingControl.UrlRewriteRule = ModuleQueryStringKey.UrlPaging;
                this.PagingControl.TotalRecord = ClientCmsArticleBO.ArticleCount;
                this.PagingControl.RecordPerPage = row_num;
                this.PagingControl.RefreshPaging();
            }
        }

        protected void Nhom_MeDia_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ClientCmsArticleAndCategoryEntity objMedia = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                if (null != objMedia)
                {
                    #region Url Chi Tiết
                    string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objMedia.Page_alias,
                                        new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                        new string[]
                                            {
                                           Convert.ToString(objMedia.Category_id),
                                           Convert.ToString(objMedia.Article_id),
                                           Functions.UnicodeToKoDauAndGach(GetCateName(objMedia.Category_id)),
                                           Functions.UnicodeToKoDauAndGach(objMedia.Article_title)
                                           }
                    );

                    #endregion
                    #region Bind Anh minh hoa cua tin
                    Image imgAnhTin = e.Item.FindControl("Anh_Minh_Hoa_Tin") as Image;
                    if (imgAnhTin != null)
                    {
                        imgAnhTin.ImageUrl = objMedia.Thumb_image;
                        imgAnhTin.Attributes.Add("onerror", "ImageNotFound(this);");
                    }
                    #endregion
                    #region Bind Tiêu đề tin
                    if (HienThiTieuDeTin)
                    {
                        HyperLink linkTieuDe = e.Item.FindControl("Tieu_De_Tin") as HyperLink;
                        if (linkTieuDe != null)
                        {
                            linkTieuDe.Text = objMedia.Article_title;
                            linkTieuDe.NavigateUrl = urlChiTiet;
                        }
                    }
                    #endregion
                    #region Bind Link Ct Tin
                    if (HienThiLinkCtTin)
                    {
                        HyperLink linkCt = e.Item.FindControl("Link_CT_Tin") as HyperLink;
                        linkCt.Text = GetLanguageValue("ReadMoreButton");
                        linkCt.NavigateUrl = urlChiTiet;
                    }

                    #endregion
                    #region Bind Mô tả tin
                    if (HienThiMoTaTin)
                    {
                        Literal ltrMoTa = e.Item.FindControl("Mo_Ta_Tin") as Literal;
                        if (ltrMoTa != null) ltrMoTa.Text = objMedia.Article_description;
                    }

                    #endregion


                    #region Find control từ danh sách media
                    Image imageAnh = e.Item.FindControl("Icon_Image") as Image;
                    Image imageVideo = e.Item.FindControl("Icon_Video") as Image;
                    Image imageFile = e.Item.FindControl("Icon_File") as Image;
                    Literal literal_img = e.Item.FindControl("scipt_img_group_var") as Literal;
                    #endregion

                    #region Bind dữ liểu icon ảnh

                    StringBuilder builder = new StringBuilder();
                    builder.Append("<script type=\"text/javascript\" charset=\"utf-8\">");

                    #region Popup Icon Image
                    IList<ClientCmsMediaEnity> listAnh =
                       ClientCmsArticleBO.GetCmsTopMediaEnitiesByArticleId(objMedia.Article_id,PortalContext.LanguageInUseId, 3,Functions.GetInt(SLPopupAnh));
                    if (listAnh.Count > 0)
                    {
                        if (imageAnh != null)
                        {
                            IList<string> list_thumb = new List<string>();
                            IList<string> list_tilte = new List<string>();
                            IList<string> list_description = new List<string>();
                            ImageColection objImg = new ImageColection();
                            objImg.ArticleId = objMedia.Article_id;
                            foreach (ClientCmsMediaEnity objM in listAnh)
                            {
                                string img_thumb = objM.MediaSrc;
                                string img_tilte = objM.MediaTitle;
                                string img_description = objM.MediaDescription;
                                list_thumb.Add(img_thumb);
                                list_tilte.Add(img_tilte);
                                list_description.Add(img_description);
                            }
                            objImg.ImageThumb = list_thumb.ToArray();
                            objImg.TilteImg = list_tilte.ToArray();
                            objImg.Description = list_description.ToArray();

                            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                            builder.Append("var objColectionImage" + objMedia.Article_id + "=" +
                                           jsSerializer.Serialize(objImg) + ";");
                            string value = string.Format("return openImages(objColectionImage{0});",
                                                         objMedia.Article_id);
                            imageAnh.Attributes.Add("onclick", value);
                        }
                    }
                    else
                    {
                        if (imageAnh != null) imageAnh.Visible = false;
                    }
                    #endregion

                    #region Popup Icon Image Video
                    IList<ClientCmsMediaEnity> listVideo =
                        ClientCmsArticleBO.GetCmsTopMediaEnitiesByArticleId(objMedia.Article_id, PortalContext.LanguageInUseId, 2, Functions.GetInt(SLPopupVideo));
                    if (listVideo.Count > 0)
                    {

                        if (imageVideo != null)
                        {
                            IList<string> list_thumb = new List<string>();
                            IList<string> list_tilte = new List<string>();
                            IList<string> list_description = new List<string>();
                            ImageColection objImg = new ImageColection();
                            objImg.ArticleId = objMedia.Article_id;
                            foreach (ClientCmsMediaEnity objM in listVideo)
                            {
                                string img_thumb = objM.MediaSrc;
                                string img_tilte = objM.MediaTitle;
                                string img_description = objM.MediaDescription;
                                list_thumb.Add(img_thumb);
                                list_tilte.Add(img_tilte);
                                list_description.Add(img_description);
                            }
                            objImg.ImageThumb = list_thumb.ToArray();
                            objImg.TilteImg = list_tilte.ToArray();
                            objImg.Description = list_description.ToArray();

                            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                            builder.Append("\n var objColectionVD" + objMedia.Article_id + "=" +
                                           jsSerializer.Serialize(objImg));
                            string value = string.Format("return openImages(objColectionVD{0});",
                                                         objMedia.Article_id);
                            imageVideo.Attributes.Add("onclick", value);

                        }
                    }
                    else
                    {
                        if (imageVideo != null) imageVideo.Visible = false;
                    }
                    #endregion

                    #region Popup Icon File
                    IList<ClientCmsMediaEnity> listFile =
                      ClientCmsArticleBO.GetCmsTopMediaEnitiesByArticleId(objMedia.Article_id, PortalContext.LanguageInUseId, 1,Functions.GetInt(SLPopupFile));
                    if (listFile.Count > 0)
                    {

                        if (imageFile != null)
                        {
                            IList<string> list_thumb = new List<string>();
                            IList<string> list_tilte = new List<string>();
                            IList<string> list_description = new List<string>();
                            ImageColection objImg = new ImageColection();
                            objImg.ArticleId = objMedia.Article_id;
                            foreach (ClientCmsMediaEnity objM in listFile)
                            {
                                string img_thumb = objM.MediaSrc;
                                string img_tilte = objM.MediaTitle;
                                string img_description = objM.MediaDescription;
                                list_thumb.Add(img_thumb);
                                list_tilte.Add(img_tilte);
                                list_description.Add(img_description);
                            }
                            objImg.ImageThumb = list_thumb.ToArray();
                            objImg.TilteImg = list_tilte.ToArray();
                            objImg.Description = list_description.ToArray();

                            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                            builder.Append("\n var objColectionFile" + objMedia.Article_id + "=" +
                                           jsSerializer.Serialize(objImg));
                            string value = string.Format("return openImages(objColectionFile{0});",
                                                         objMedia.Article_id);
                            imageFile.Attributes.Add("onclick", value);

                        }
                    }
                    else
                    {
                        if (imageFile != null) imageFile.Visible = false;
                    }

                    #endregion

                    builder.Append("</script>");
                    if (literal_img != null) literal_img.Text = builder.ToString();
                    #endregion
                }
            }
        }
    }
}